const router = require("koa-router")();
const main = require("../models")
router.post("/doLogin", async ctx => {
    console.log(ctx.request.body);
    var { username, pwd } = ctx.request.body;
    var userCollection = await main("user");
    var res = await userCollection.find({ username,pwd }).toArray();
    console.log(res);
    if (res.length) {
        ctx.cookies.set("login","success")
        await ctx.redirect("/form")
    } else {
        ctx.body = "<script>alert('用户名或密码错误');location.href='/login'</script>"
    }
    var result = await userCollection.find({ username}).toArray();
    if(!result.length){
        ctx.body = "<script>alert('用户名未注册，请先注册');location.href='/register'</script>"
    }
})
module.exports = router;